package mysql

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var DB *gorm.DB
var err error

func Init() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := "root:mysql_i2km3e@tcp(14.103.156.92:3307)/zk1_zg1?charset=utf8mb4&parseTime=True&loc=Local"
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Println("数据库连接成功")
	}

	err := DB.AutoMigrate(&User{}, &House{})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Println("数据库连接成功")
	}
}
